package com.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebFilter("/*")
public class FilterAll implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        /*String username = request.getParameter("username");不能这样拿username值*/
        Object username = request.getSession().getAttribute("username");
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        String requestURI = request.getRequestURI();
        if (requestURI.endsWith("ServletLogin")||requestURI.endsWith("new.jsp")||requestURI.endsWith("index.jsp")||requestURI.endsWith("New")){
            filterChain.doFilter(request,response);
            return;
        }
        if (username!=null){
            filterChain.doFilter(request,response);
            return;
        }else{
            response.getWriter().write("请登陆..");
            response.setHeader("Refresh","2;URL=index.jsp");
            return;
        }
    }
}
